<template>
  <div>
    <Spin :spinning="exportLoading" tip="Loading...">
      <FormPanel ref="from" :list="formList" @formChange="changeHandle" :cols="2" :isSubmitBtn="false" :collapse="false" labelWidth="110" />
      <div style="height: 47px"></div>
      <div
        :style="{
          position: 'absolute',
          left: 0,
          bottom: 0,
          right: 0,
          zIndex: 9,
          borderTop: '1px solid #e9e9e9',
          padding: '10px 20px',
          background: '#fff',
          textAlign: 'right',
        }"
      >
        <el-button @click="closeDrawer(1)">关 闭</el-button>
        <multiuse-button type="primary" :click="closeDrawer.bind(this, 2)">确 认</multiuse-button>
      </div>
    </Spin>
  </div>
</template>
<script>
import { getDealerAddress, confirmAddressAdjust } from '@/api/omSale/OrderManagement/OrderAuditConfirm';
export default {
  data() {
    this.saveData = null;
    return {
      formList: this.createdFrom(),
      exportLoading: false,
      addressDTO: [],
    };
  },
  props: ['list'],
  created() {
    this.getList(this.list);
  },
  methods: {
    createdFrom() {
      return [
        {
          type: 'SELECT',
          label: '收车地址',
          fieldName: 'addressId',
          placeholder: '请选择',
          itemList: [],
          rules: [{ required: true, message: '请选择', trigger: 'change' }],
        },
      ];
    },
    async getList(list) {
      let res = await getDealerAddress({ dealerCode: list[0].dealerCode });
      this.addressDTO = res.data;
      this.formList.find((x) => x.fieldName == 'addressId').itemList = res.data.map((x) => ({ text: x.address, value: x.addressId }));
    },
    changeHandle(val) {
      this.saveData = val;
    },
    async closeDrawer(type) {
      if (type == 1) {
        this.$emit('close');
      } else {
        this.$refs.from.SUBMIT_FORM();
        this.exportLoading = true;
        if (this.saveData) {
          let json = JSON.parse(JSON.stringify(this.saveData));
          let arr = [];
          this.list.map((x) => {
            arr.push(x.id);
          });
          json.ids = arr;
          json.id = json.addressId;
          for (let i in this.addressDTO) {
            if (this.addressDTO[i].addressId === json.addressId) {
              json.address = this.addressDTO[i].address;
              json.linkMan = this.addressDTO[i].linkMan;
              json.linkPhone = this.addressDTO[i].linkPhone;
              json.cityName = this.addressDTO[i].cityName;
            }
          }

          let res = await confirmAddressAdjust(json);
          this.exportLoading = false;
          if (res.resultCode == 200) {
            let total = res.data.total;
            let errorTotal = res.data.errorTotal;
            let error = res.data.error;
            this.$notify({
              title: '地址调整状态',
              message: '地址调整：保存' + total + '条,失败' + errorTotal + '条,失败列表' + error,
              type: 'success',
            });
            // this.$notify({
            //   title: '地址调整状态',
            //   message: '地址调整成功',
            //   type: 'success',
            // });
            this.$emit('close', json);
          } else {
            this.saveData = null;
            this.exportLoading = false;
          }
        }
      }
    },
  },
};
</script>
